#include "minimap.h"

MiniMap::MiniMap(QPixmap map, QList<Building *> *buildingList, QGraphicsScene *scene, QGraphicsView *view)
    : QGraphicsPixmapItem(map)
{
    if ((200.0/pixmap().width()) < (130.0/pixmap().height()))
        setScale(200.0/pixmap().width());    // This has to be 200.0 or it will do integer division and will resuly in a 1 or 0
    else
        setScale(130.0/pixmap().height());

    scene->addItem(this);

    parentView = view;
    buildings = buildingList;

    screenRect = new ScreenRectItem(this,scene);

    screenRect->setRect(0,0,parentView->width()-2,parentView->height()-152);

    startTimer(500); // Start a timer to repaint the minimap at least every half second
}

void MiniMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QGraphicsPixmapItem::paint(painter,option,widget);

    painter->setPen(Qt::NoPen);

    for (int index = 0; index < buildings->length(); index++)
    {
        if (buildings->at(index)->isInfected())
            painter->setBrush(QBrush(Qt::red,Qt::SolidPattern));
        else
            painter->setBrush(QBrush(Qt::white,Qt::SolidPattern));

        painter->drawRect(buildings->at(index)->pos().x()+(buildings->at(index)->boundingRect().width()/2)-(2/scale()),
                          buildings->at(index)->pos().y()+(buildings->at(index)->boundingRect().width()/2)-(2/scale()),
                          (4/scale()),(4/scale()));
    }
}

void MiniMap::timerEvent(QTimerEvent *event)
{
    update(boundingRect());
}
